home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / checkchar.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  143 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *      checkchar -
  19.  *              Test a character outline for self intersections
  20.  *
  21.  *                              Paul Haeberli - 1990
  22.  *
  23.  *    exports
  24.  *
  25.     int checkchar(fnt,c)
  26.  *
  27.  */
  28. #include "stdio.h"
  29. #include "objfnt.h"
  30. #include "vect.h"
  31.  
  32. #define MAXPOS    2000
  33. static int nedge;
  34. static float edge[MAXPOS][4];
  35.  
  36. static double triarea(p00,p01,p10)
  37. vect *p00,*p01,*p10;
  38. {
  39.     double tz;
  40.  
  41.     tz = (p10->x - p00->x) * (p10->y + p00->y);
  42.     tz += (p00->x - p01->x) * (p00->y + p01->y);
  43.     tz += (p01->x - p10->x) * (p01->y + p10->y);
  44.     return tz;
  45. }
  46.  
  47. static int linecrosses(e1,e2)
  48. float *e1, *e2;
  49. {
  50.     double a1, a2;
  51.  
  52.     a1 = triarea(e1+0,e1+2,e2+0);
  53.     a2 = triarea(e1+0,e1+2,e2+2);
  54.     if((a1*a2)<0.0) 
  55.     return 1;
  56.     else
  57.     return 0;
  58. }
  59.  
  60. static int checkinter(e1,e2)
  61. float *e1, *e2;
  62. {
  63.     if(e1[0] == e2[0] && e1[1] == e2[1])
  64.     return 0;
  65.     if(e1[0] == e2[2] && e1[0] == e2[3])
  66.     return 0;
  67.     if(e1[2] == e2[0] && e1[3] == e2[1])
  68.     return 0;
  69.     if(e1[2] == e2[2] && e1[3] == e2[3])
  70.     return 0;
  71.     if(linecrosses(e1,e2) && linecrosses(e2,e1))
  72.     return 1;
  73.     return 0;
  74. }
  75.  
  76. static void clearedge()
  77. {
  78.    nedge = 0;
  79. }
  80.  
  81. static void addedge(p1,p2)
  82. short *p1, *p2;
  83. {
  84.     if(nedge>=MAXPOS) {
  85.      fprintf(stderr,"Increase MAXPOS\n");
  86.      exit(1);
  87.     }
  88.     edge[nedge][0] = p1[0];
  89.     edge[nedge][1] = p1[1];
  90.     edge[nedge][2] = p2[0];
  91.     edge[nedge][3] = p2[1];
  92.     nedge++;
  93. }
  94.  
  95. static int checkedge(c)
  96. int c;
  97. {
  98.     int i, j, n, bad;
  99.  
  100.     bad = 0;
  101.     for(i=0; i<nedge; i++) {
  102.     if(edge[i][0] == edge[i][2] && edge[i][1] == edge[i][3]) {
  103.         fprintf(stderr,"char %d zero length edge %d\n",c,i,j);
  104.         bad++;
  105.     }
  106.     n = 0;
  107.     for(j=i; j<nedge; j++) {
  108.         if(edge[i][0] == edge[j][0] && edge[i][1] == edge[j][1])
  109.         n++;
  110.         if(checkinter(edge[i],edge[j])) {
  111.         fprintf(stderr,"char %d edge intersect %d with %d\n",c,i,j);
  112.         fprintf(stderr,"edge1: %f %f to %f %f\n",
  113.                edge[i][0],edge[i][1],edge[i][2],edge[i][3]);
  114.         fprintf(stderr,"edge2: %f %f to %f %f\n",
  115.                edge[j][0],edge[j][1],edge[j][2],edge[j][3]);
  116.         bad++;
  117.         }
  118.     }
  119.     if(n != 1) {
  120.         fprintf(stderr,"char %d coincident point %f %f n is %d\n",c,edge[i][0],edge[i][1],n);
  121.         bad++;
  122.     }
  123.     }
  124.     return bad;
  125. }
  126.  
  127. int checkchar(fnt,c)
  128. objfnt *fnt;
  129. int c;
  130. {
  131.     chardesc *cd;
  132.  
  133.     cd = getchardesc(fnt,c);
  134.     if(!cd)
  135.     return;
  136.     if(cd->data) {
  137.     clearedge();
  138.     applytocharedges(fnt,c,addedge);
  139.     return checkedge(c);
  140.     }
  141.     return 0;
  142. }
  143.